﻿using Microsoft.AspNetCore.Mvc.Filters;

namespace B.S.GridManage.Api.Read.Filters
{
    public class MyExceptionFilter : IExceptionFilter
    {
        //Nlog日志组件
        private readonly ILogger<MyExceptionFilter> logger;

        public MyExceptionFilter(ILogger<MyExceptionFilter> logger)
        {
            this.logger = logger;
        }
          
        /// <summary>
        /// 全局异常过滤器的处理方法,只要有异常,该方法就会被调用
        /// </summary>
        /// <param name="context">异常上下文</param>
        /// <exception cref="NotImplementedException"></exception>
        public void OnException(ExceptionContext context)
        {
            //1.记录  2.处理
            logger.LogError(context.Exception.Message);
            context.ExceptionHandled = true;
        }
    }
}
